//
//  HMHomeViewController.m
//  HMWeiBoOC
//
//  Created by GD on 16/9/17.
//  Copyright © 2016年 geduo. All rights reserved.
//

#import "HMHomeViewController.h"
#import "UIBarButtonItem+Extention.h"
#import "HMUserAccountViewModel.h"
#import "HMStatusListViewModel.h"
#import "HMHomeTableViewCell.h"
#import "UILabel+HMLabel.h"

#import "HMWeiBoOC-Swift.h" 

static NSString *cellID = @"homeViewController";

@interface HMHomeViewController ()<UITableViewDataSource>
//上拉
@property (nonatomic,strong) UIActivityIndicatorView *pullUpView;


//下拉
@property (nonatomic,strong) HMRefreshControl *pullDownView;

@property (nonatomic,strong) UILabel *tipLabel;

@end

@implementation HMHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    BOOL isLogin = [HMUserAccountViewModel sharedViewModel].isLogin;
    if (!isLogin) {
        NSLog(@"%@ ---%zd",self,isLogin);
        [self.visitorView updateVisitorInfo:nil andImageName:nil];
    }else{
        [self loadData];
        [self.tableView registerClass:[HMHomeTableViewCell class] forCellReuseIdentifier:cellID];
//        self.tableView.rowHeight = 200;
        self.tableView.rowHeight = UITableViewAutomaticDimension;
        self.tableView.estimatedRowHeight = 200;
        
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        
        self.tableView.tableFooterView = self.pullUpView;//为什么不显示??
        
        [self.tableView addSubview:self.pullDownView];
        
        [self setupUI];
        
    }
}
-(void)setupUI{
    UINavigationController *nav = self.navigationController;
    [nav.view insertSubview:self.tipLabel belowSubview:nav.navigationBar];
    
    self.tipLabel.frame = CGRectMake(0, CGRectGetMaxY(nav.navigationBar.frame)-35, nav.navigationBar.frame.size.width, 35);
}

-(void)loadData{
//    [[HMStatusListViewModel shareManager]];
    [[HMStatusListViewModel shareManager] loadData:self.pullUpView.isAnimating WithCallBack:^(BOOL isSuccess,NSString *message) {
        if (isSuccess) {
            NSLog(@"数据回来了");
            [self.tableView reloadData];
            
//            [self.pullUpView stopAnimating];
            
        }
        if (!self.pullUpView.isAnimating) {
            [self startAnimation:message];
        }
        
        [self endRefreshing];
    }];
    
}

-(void)startAnimation:(NSString *)message{
    if (self.tipLabel.hidden == NO) {
        return;
    }
    
    self.tipLabel.text = message;
    self.tipLabel.hidden = NO;
    
    [UIView animateWithDuration:1 animations:^{
        self.tipLabel.transform = CGAffineTransformMakeTranslation(0, self.tipLabel.frame.size.height);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1 animations:^{
            
            self.tipLabel.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            self.tipLabel.hidden = YES;
        }];
    }];
}



#pragma mark -UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [HMStatusListViewModel shareManager].statusList.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HMHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    
//    cell.backgroundColor = [UIColor darkGrayColor];
    
    cell.statusViewModel = [HMStatusListViewModel shareManager].statusList[indexPath.row];
    
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    
//    cell.textLabel.text = [[HMStatusListViewModel shareManager].statusList[indexPath.row] text];
    
    
    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

    //判断为最后一个cell 通过数组来判断  不写&&  会不断第发送请求
    if (indexPath.row == [HMStatusListViewModel shareManager].statusList.count - 1 && !self.pullUpView.isAnimating) {
        NSLog(@"最后一个cell了");
        [self.pullUpView startAnimating];
        
        [self loadData];
    }
}


//懒加载
-(UIActivityIndicatorView *)pullUpView{
    if (_pullUpView == nil) {
        _pullUpView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        _pullUpView.color = [UIColor redColor];
//        _pullDownView.frame.size = CGSizeMake(100, 35);
//        _pullDownView.frame = CGRectMake(0, 0, 100, 35);
    }
    return _pullUpView;
}

-(HMRefreshControl *)pullDownView{
    if (!_pullDownView) {//懒加载??  代码规范!!!
        _pullDownView = [[HMRefreshControl alloc] init];
        [_pullDownView addTarget:self action:@selector(pullDownRefreshing) forControlEvents:UIControlEventValueChanged];
    }
    return _pullDownView;
}

-(void)endRefreshing{
    [self.pullUpView stopAnimating];
    
    [self.pullDownView endRefreshing];
}

-(void)pullDownRefreshing{
    NSLog(@"下拉刷新");
    [self loadData];
}

//Label
-(UILabel *)tipLabel{
    if (_tipLabel == nil) {
        _tipLabel = [[UILabel alloc] initWithTextColor:[UIColor darkGrayColor] fontSize:[UIFont systemFontOfSize:12]];
        _tipLabel.backgroundColor = [UIColor orangeColor];
        _tipLabel.textAlignment = NSTextAlignmentCenter;
        _tipLabel.text = @"没有加载到最新的微博数据";
        _tipLabel.hidden = YES;
    }
    return _tipLabel;
}


@end
